<?php 
require ('../class/admin/sektoral.php');
$db = new User();
$db2 = new Sektoral();
$table   = "pegawai";
$table2   = "sektoral";
$table3   = "user";
$pegawai 	= $db->lihat_pegawai($table);
$sektoral	= $db2->lihat($table2, "idsektoral","") ;
?>	

<script type="text/javascript">
	function lookup(nip) {
		if(nip.length == 0) {
			// Hide the suggestion box.
			$('#suggestions').hide();
		} else {
			$.post("getpegawai.php", {queryString: ""+nip+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
		}
	} // lookup
	
	function fill(thisValue1,thisValue2) {
		$('#nama').val(thisValue1);
		$('#nip').val(thisValue2);
		setTimeout("$('#suggestions').hide();", 200);
	}
</script>

<form class="form-horizontal" id="registerHere" method='post' action='?page=adduser&action=add'>
  <fieldset>
	<legend>TAMBAH USER</legend>
	<?php

	   if (isset($_GET['success']) && ($_GET['success'] == "1")) {

			$o .= '<div class="alert alert-success">
					Proses tambah user berhasil, kode aktifasi dan data login sudah dikirimkan ke email user
					</div>';
					
		} elseif (isset($_GET['success']) && ($_GET['success'] == "0")) {
			
			$o .= '<div class="alert alert-error">
					Proses tambah user gagal
				   </div>';
		}
		
	echo $o;
	
	?>
	
	<div class="control-group">
	  <label class="control-label" for="input01">NIP</label>
	  <div class="controls">
		<input type="text" class="input-xlarge" id="nip" onkeyup="lookup(this.value);" onblur="fill();" name="nip" rel="popover" data-content="Masukkan NIP" data-original-title="NIP">
		<div class="suggestionsBox" id="suggestions" style="display: none;">
				<img src="../assets/img/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
				<div class="suggestionList" id="autoSuggestionsList">
					&nbsp;
				</div>
		</div>
	  </div>
	</div>
	
	<div class="control-group">
	  <label class="control-label" for="input01">Nama Pegawai</label>
	  <div class="controls">
		<input type="text" class="input-xlarge" id="nama" name="nama" rel="popover" data-content="Masukkan Nama Pegawai" data-original-title="Nama Pegawai">
	  </div>
	</div>
	
	<div class="control-group">
	  <label class="control-label" for="input01">Sektoral</label>
	  <div class="controls">
		<select name="sektoral" id="sektoral" data-placeholder="Pilih sektoral..." class="chzn-select" style="width:280px;" tabindex="2">
		<?php
		
		/* menampilkan data sektoral */
		if ($sektoral) {
			$o2 .= '<option value=""></option>';
			foreach ($sektoral as $y) {
				$o2 .= '<option value='.$y['idsektoral'].'>'.$y['nama_sektoral'].'</option>';
			}
		} else {
			$o2 .= '<option value="">tidak ada data sektoral</option>';
		}
		
		echo $o2;
	
		?>
		</select>

		</div>
	</div>
	
	
	<div class="control-group">
	  <label class="control-label" for="input01">Username</label>
	  <div class="controls">
		<input type="text" class="input-xlarge" id="username" name="username" rel="popover" data-content="Masukkan username." data-original-title="Username">
	  </div>
	</div>

 <div class="control-group">
	<label class="control-label" for="input01">Email</label>
	  <div class="controls">
		<input type="text" class="input-xlarge" id="email" name="email" rel="popover" data-content="Masukkan alamat email" data-original-title="Email">
	   
	  </div>
</div>

<div class="control-group">
	<label class="control-label" for="input01">Password</label>
	  <div class="controls">
		<input type="password" class="input-xlarge" id="password" name="password" rel="popover" data-content="6 characters or more! Be tricky" data-original-title="Password" >       
	  </div>
</div>

<div class="control-group">
	<label class="control-label" for="input01">Konfirmasi Password</label>
	  <div class="controls">
		<input type="password" class="input-xlarge" id="confirmpass" name="confirmpass" rel="popover" data-content="Re-enter your password for confirmation." data-original-title="Re-Password" >
	  </div>
</div>

<div class="control-group">
	<label class="control-label" for="input01"></label>
	  <div class="controls">
	   <button type="submit" class="btn btn-success" rel="tooltip" title="first tooltip">Tambah User</button>
	  </div>
</div>
  </fieldset>
</form>

<a href="?page=user">kembali</a>

<?php


if ($_GET['action'] == "add") {
	
	require ('../class/hex.php');
	
	$nip 		= htmlentities(mysql_real_escape_string($_POST['nip']));
	$username	= htmlentities(mysql_real_escape_string($_POST['username']));
	$email		= htmlentities(mysql_real_escape_string($_POST['email']));
	$sektoral	= htmlentities(mysql_real_escape_string($_POST['sektoral']));
	$pass		= encode(htmlentities(mysql_real_escape_string($_POST['password'])));
	$level		= "Operator";
	$hash 		= md5(rand(0,1000));
	$active		= 0;
	$user 		= array('username'=>$username, 
						'password'=>$pass,
						'nip'=>$nip,
						'email'=>$email,
						'hak_akses'=>$level,
						'idsektoral'=>$sektoral,
						'kode_aktifasi'=>$hash,
						'aktif'=>$active);
						
	$adduser = $db->tambah($table3, $user);
	
	if ($adduser) {
	
		$to   = $email; 
				$subject = 'Pendaftaran | Verifikasi User'; //// Give the email a subject 
				$message = '

				Akun anda telah berhasil dibuat, anda dapat masuk ke halaman operator dengan data akun dibawah ini setelah anda mengaktifasi akun anda.

				------------------------
				Username: '.$username.'
				Password: '.$_POST['password'].'
				------------------------

				Silahkan klik tautan dibawah ini untuk mengaktifasikan akun anda:
				http://localhost/gis/auth/verifikasi.php?email='.$email.'&code='.$hash.'

				'; 
					
					//$headers = 'From:noreply@yourwebsite.com' . "\r\n"; 
					mail($to, $subject, $message); 
					
		?>
		<script language="javascript">
		document.location="?page=adduser&success=1";
		</script>
		<?php
		$o .= '<div class="alert alert-success">
				Proses tambah user berhasil
			   </div>';
	} else {
		?>
			<script language="javascript">
			document.location="?page=adduser&success=0";
			</script>
		<?php
		$o .= '<div class="alert alert-success">
				Proses tambah user gagal
			   </div>';
	}
	
}





